package com.sc.servlet.admin;

import com.sc.services.FieldServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @description: 管理员登陆
 * @author: sc
 * @create: 2023-01-02 17:12
 **/
@WebServlet("/adminlogin")
public class loginServlet extends HttpServlet {
    private FieldServices fieldServices = new FieldServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset = utf-8");
        HttpSession session = request.getSession();


        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        boolean loginAdmin = fieldServices.isLoginAdmin(username, pwd);
        if(loginAdmin){
            session.setAttribute("adminname",username);
            response.sendRedirect("/FieldBookSystem/admin.html");
        }
        else{
            request.setAttribute("password_err","用户名或密码错误");
            request.getRequestDispatcher("/adminLogin.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}